package com.example.lib_cache.viewmodel;

import android.content.Context;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.baidu.mapapi.model.LatLng;
import com.example.lib_cache.model.Application;
import com.example.lib_cache.model.Guide;
import com.example.lib_cache.model.Navigation;
import com.example.lib_cache.model.Order;
import com.example.lib_cache.model.User;
import com.example.lib_cache.network.exception.ApiException;
import com.example.lib_cache.network.exception.ErrorConsumer;
import com.example.lib_cache.network.response.ResponseResultTransformer;
import com.example.lib_cache.service.ApiService;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;

public class PickUpActivityViewModel extends ViewModel {
    private final static String TAG = PickUpActivityViewModel.class.getSimpleName();

    private MutableLiveData<Order> orderMutableLiveData = new MutableLiveData<>();
    private MutableLiveData<Guide> guideMutableLiveData = new MutableLiveData<>();
    private MutableLiveData<Navigation> navigationMutableLiveData = new MutableLiveData<>();
    private LatLng orderLL = null;
    private LatLng newLL = null;
    private boolean swap = false;
    private MutableLiveData<Boolean> isPickUp = new MutableLiveData<>();
    private MutableLiveData<String> driverAccount = new MutableLiveData<>();

    private MutableLiveData<User> userMutableLiveData = new MutableLiveData<>();
    private MutableLiveData<Application> applicationMutableLiveData = new MutableLiveData<>();

    public MutableLiveData<User> getUserMutableLiveData() {
        return userMutableLiveData;
    }

    public void setUserMutableLiveData(User user) {
        this.userMutableLiveData.postValue(user);
    }

    public MutableLiveData<Application> getApplicationMutableLiveData() {
        return applicationMutableLiveData;
    }

    public void setApplicationMutableLiveData(Application application) {
        this.applicationMutableLiveData.postValue(application);
    }

    public MutableLiveData<Order> getOrderMutableLiveData() {
        return orderMutableLiveData;
    }

    public void setOrderMutableLiveData(Order order) {
        this.orderMutableLiveData.postValue(order);
    }

    public MutableLiveData<Guide> getGuideMutableLiveData() {
        return guideMutableLiveData;
    }

    public void setGuideMutableLiveData(Guide guide) {
        this.guideMutableLiveData.postValue(guide);
    }

    public MutableLiveData<Navigation> getNavigationMutableLiveData() {
        return navigationMutableLiveData;
    }

    public MutableLiveData<String> getDriverAccount() {
        return driverAccount;
    }

    public void setDriverAccount(String driverAccount) {
        this.driverAccount.postValue(driverAccount);
    }

    public void setNavigationMutableLiveData(Navigation navigation) {
        this.navigationMutableLiveData.postValue(navigation);
    }

    public LatLng getOrderLL() {
        return orderLL;
    }

    public LatLng getNewLL() {
        return newLL;
    }

    public boolean getSwap() {
        return swap;
    }

    public void setSwap(boolean toSwap) {
        this.swap = toSwap;
    }

    public MutableLiveData<Boolean> getIsPickUp() {
        return isPickUp;
    }

    public void setIsPickUp(boolean isPickUp) {
        this.isPickUp.postValue(isPickUp);
    }

    public void swapLatLng(Double longitude, Double latitude){
        if(swap) {
            orderLL = newLL;
            newLL = new LatLng(latitude, longitude);
            return;
        }
        orderLL = new LatLng(latitude,longitude);
        newLL = new LatLng(latitude,longitude);
    }



    //获取用户正在进行的导航信息
    public void requestNavigation(ApiService apiService, Context context){
        apiService.getWaitingNavigation()
            .compose(ResponseResultTransformer.<Navigation>obtain())
            .subscribe(new Consumer<Navigation>() {
                @Override
                public void accept(Navigation navigation) throws Exception {
                    Log.d(TAG,"得到导航信息");
                    setNavigationMutableLiveData(navigation);
                }
            }, new ErrorConsumer(context) {
                @Override
                protected void error(ApiException ex) {
                    Log.d(TAG,"获取导航信息失败");
                    Log.d(TAG,ex.getCode());
                    Log.d(TAG,ex.getErrorMsg());
                }
            });
    }

    //获取用户发出的订单信息
    public void requestOrder(final ApiService apiService, final Context context){
        apiService.getSendingOrderStatus()
                .compose(ResponseResultTransformer.<Order>obtain())
                .subscribe(new Consumer<Order>() {
                    @Override
                    public void accept(Order order) throws Exception {
                        Log.d(TAG,"得到订单信息");
                        setOrderMutableLiveData(order);
                        //加载到订单信息后执行
                        requestDriverLocation(apiService,context);
                    }
                }, new ErrorConsumer(context) {
                    @Override
                    protected void error(ApiException ex) {
                        Log.d(TAG,"获取订单信息失败");
                        Log.d(TAG,ex.getCode());
                        Log.d(TAG,ex.getErrorMsg());
                    }
                });
    }

    //获取司机位置
    public void requestDriverLocation(ApiService apiService,Context context){
        apiService.getDriverLocation(orderMutableLiveData.getValue().getOrderId())
                .compose(ResponseResultTransformer.<Guide>obtain())
                .subscribe(new Consumer<Guide>() {
                    @Override
                    public void accept(Guide guide) throws Exception {
                        Log.d(TAG,"得到司机位置信息");
                        setGuideMutableLiveData(guide);
                    }
                }, new ErrorConsumer(context) {
                    @Override
                    protected void error(ApiException ex) {
                        Log.d(TAG,"获取司机位置信息失败");
                        Log.d(TAG,ex.getCode());
                        Log.d(TAG,ex.getErrorMsg());
                    }
                });
    }

    //获取司机账号
    public void requestDriverAccount(ApiService apiService,Context context){
        apiService.getDriverUserName()
                .compose(ResponseResultTransformer.<String>obtain())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        Log.d(TAG,"成功返回司机用户名");
                        setDriverAccount(s);
                    }
                }, new ErrorConsumer(context) {
                    @Override
                    protected void error(ApiException ex) {
                        Log.d(TAG,"返回司机用户名失败");
                        Log.d(TAG,ex.getCode());
                        Log.d(TAG,ex.getErrorMsg());
                    }
                });
    }

    public void requestPickUp(ApiService apiService,Context context){
        apiService.orderWithPickUp(orderMutableLiveData.getValue().getOrderId())
                .compose(ResponseResultTransformer.<Long>obtain())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        Log.d(TAG,"修改订单状态成功");
                        setIsPickUp(true);
                    }
                }, new ErrorConsumer(context) {
                    @Override
                    protected void error(ApiException ex) {
                        Log.d(TAG,"修改订单状态失败！");
                        Log.d(TAG,ex.getCode());
                        Log.d(TAG,ex.getErrorMsg());
                    }
                });
    }

    public void requestApplication(ApiService apiService,Context context,Long driverId){
        apiService.getDriverCarInfo(driverId)
                .compose(ResponseResultTransformer.<Application>obtain())
                .subscribe(new Consumer<Application>() {
                    @Override
                    public void accept(Application application) throws Exception {
                        Log.d(TAG,"请求成功");
                        setApplicationMutableLiveData(application);
                    }
                }, new ErrorConsumer(context) {
                    @Override
                    protected void error(ApiException ex) {
                        Log.d(TAG,"请求失败");
                        Log.d(TAG,ex.getCode());
                        Log.d(TAG,ex.getErrorMsg());
                        if("201".equals(ex.getCode())){
                        }
                    }
                });
    }

    public void requestPersonInfo(ApiService apiService,Context context,Long userId){
        apiService.getUser(userId).compose(ResponseResultTransformer.<User>obtain())
                .subscribe(new Consumer<User>() {
                    @Override
                    public void accept(User user) throws Exception {
                        Log.d(TAG,"请求成功");
                        setUserMutableLiveData(user);
                    }
                }, new ErrorConsumer(context) {
                    @Override
                    protected void error(ApiException ex) {
                        Log.d(TAG,"请求失败");
                        Log.d(TAG,ex.getCode());
                        Log.d(TAG,ex.getErrorMsg());
                        if("201".equals(ex.getCode())){
                        }
                    }
                });
    }
}
